package com.duguo.dynamicmvc.model.validator.exception;

import java.util.LinkedHashMap;
import java.util.Map;

import com.duguo.dynamicmvc.factory.utils.BeanUtils;

public class ValidationException extends Exception {
	
	private Map<String,ValidationResult> results=new LinkedHashMap<String,ValidationResult>();

	
	public ValidationException(){
	}

	public Map<String,ValidationResult> getResults() {
		return results;
	}
	
	public void addResult(String field,ValidationResult result) {
		results.put(field,result);
	}
	
	public void addResult(String field,ValidationException ex) {
		results.put(field,ex.getResults().values().iterator().next());
	}
	
	public boolean isInvalid(String field) {
		return results.containsKey(field);
	}
	
	public ValidationResult getValidationResult(String field) {
		return results.get(field);
	}

	public String getMessage() {
		StringBuffer msg=new StringBuffer();
		for(String field:results.keySet()){
			if(msg.length()>0)
				msg.append(",");
			ValidationResult result=results.get(field);
			msg.append(field);
			msg.append("[");
			msg.append(result.getValidator().getName());
			msg.append(",[");
			BeanUtils.inspectObject(msg,result.getValue());
			msg.append("]]");
		}
		return msg.toString();
	}
	
	public String toString(){
		return getMessage();
	}
	
	
}
